package com.learnkitchen.learnkitchenrecipes.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import java.util.Stack;

/**
 * Created by Administrator on 2018-01-03.
 */

public class ActivityManager {

    private static Stack<Activity> activityStack;
    private static ActivityManager instance;

    private ActivityManager() {
    }

    public static ActivityManager getInstance() {
        if (instance == null) {
            synchronized (ActivityManager.class) {
                instance = new ActivityManager();
            }
        }
        return instance;
    }

    /**
     * 添加Activity到堆栈
     *
     * @param activity
     */
    public void pushActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
    }

    /**
     * 删除Activity到堆栈
     *
     * @param activity
     */
    public void popActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
        }

    }

    /**
     * 获取当前Activity(堆栈中最后一个压入的)
     *
     * @return
     */
    public Activity currentActivity() {
        Activity activity = activityStack.lastElement();
        return activity;
    }

    /**
     * 获取activity
     *
     * @return
     */
    public Activity getActivity(Class<?> cls) {
        for (Activity activity : activityStack) {

//            LLog.e("AM",activity.getClass().getSimpleName());
            if (activity.getClass().getSimpleName().equals(cls.getSimpleName())) {
                return activity;
            }

        }
        return null;
    }

    /**
     * 结束当前Activity(堆栈中最后一个压入的)
     */
    public void finishLastActivity() {
        Activity activity = activityStack.lastElement();
        finishActivity(activity, Activity.RESULT_CANCELED);
    }

    /**
     * 结束指定的Activity
     *
     * @param activity
     */
    public void finishActivity(Activity activity, int resultCode) {
        if (activity != null) {
            activityStack.remove(activity);
            activity.setResult(resultCode);
            activity.finish();// 去除解决屏幕旋转的问题
            activity = null;
        }
    }

    /**
     * 结束指定的Activity
     *
     * @param activity
     */
    public void finishActivity(Activity activity, int resultCode, Intent data) {
        if (activity != null) {
            activityStack.remove(activity);
            activity.setResult(resultCode, data);
            activity.finish();// 去除解决屏幕旋转的问题
            activity = null;
        }
    }

    /**
     * 结束指定类名的Activity
     *
     * @param cls
     */
    public void finishActivity(Class<?> cls) {
//        LLog.e("AM",cls.getSimpleName());
        for (Activity activity : activityStack) {

//            LLog.e("AM",activity.getClass().getSimpleName());
            if (activity.getClass().getSimpleName().equals(cls.getSimpleName())) {
                finishActivity(activity, Activity.RESULT_CANCELED);
            }

        }
    }

    /**
     * 结束指定类名的Activity
     *
     * @param cls
     */
    public void finishActivity(Class<?> cls, int resaultCode, Intent data) {
//        LLog.e("AM",cls.getSimpleName());
        for (Activity activity : activityStack) {

//            LLog.e("AM",activity.getClass().getSimpleName());
            if (activity.getClass().getSimpleName().equals(cls.getSimpleName())) {
                finishActivity(activity, resaultCode, data);
            }

        }
    }

    /**
     * 结束指定类名的Activity
     *
     * @param cls
     */
    public void finishActivity(Class<?> cls, int resaultCode) {
//        LLog.e("AM",cls.getSimpleName());
        for (Activity activity : activityStack) {

//            LLog.e("AM",activity.getClass().getSimpleName());
            if (activity.getClass().getSimpleName().equals(cls.getSimpleName())) {
                finishActivity(activity, resaultCode);
            }

        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        for (int i = 0, size = activityStack.size(); i < size; i++) {
            if (null != activityStack.get(i)) {
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }

    /**
     * 获取总共Activity数量
     */
    public int getActivitySize() {
        if (activityStack != null) {
            return activityStack.size();
        }
        return 0;

    }

    /**
     * 退出应用程序
     */
    public void appExit(Context context) {
        try {
            finishAllActivity();
            android.app.ActivityManager activityMgr = (android.app.ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.restartPackage(context.getPackageName());
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}